<?php
/*
 * Serie anhand der ID(s) anzeigen..
 */
function ShowSeries($league_id)
{
    require_once('Database/Database.php');
    $db = new Database();
    $db->Connect();
    $series = $db->Select('Series', 'League_ID = ' . $league_id);
    $db->Disconnect();
    if($series)
    {
        $series_string = '';
        for($i = 0; $i < sizeof($series); $i++)
        {
            $series_string .= '<a href="Series/Admin/index.php?id=' . $series[$i]->ID_Series . '" target="_blank">' .$series[$i]->Shortcut .
                    '</a>';
            if($i + 1 < sizeof($series))
            {
                $series_string .= ', ';
            }
        }

        return $series_string;
    }
    else
    {
        return 'Keine Serie';
    }
}

/*
 * Listet alle Ligen auf..
 */
require_once('Database/Database.php');

$db = new Database();
$db->Connect();

try
{
    $data = $db->Select('Leagues');
}
catch(Exception $ex)
{
    echo 'No leagues found.';
    echo $ex;
}
$db->Disconnect();
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Alle Ligen</title>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div id="header">HEADER</div>
        <div id="list">
        <table>
        <tr>
        <td>Liga</td>
        <td>Kürzel</td>
        <td>Simulation</td>
        <td>Serien</td>
        <td>Ändern</td>
        <td>Löschen</td>
        </tr>
        <?php
        if($data != null)
        {
            $edit_icon   = 'Icons/edit.png';
            $edit_link   = 'Leagues/edit.php';
            $delete_icon = 'Icons/delete.png';
            $delete_link = 'Leagues/delete.php';
            foreach($data as $d)
            {
                $edit_string   = '<td><a href="' . $edit_link . '?id=' . $d->ID_League .
                    '" target="_blank"><img src="' . $edit_icon . '"></img></a></td>';

                $delete_string = '<td><a href="' . $delete_link . '?id=' . $d->ID_League .
                    '" target="_blank"><img src="' . $delete_icon . '"></img></a></td>';

                echo '<tr>';
                echo '<td>' . $d->Name . '</td>';
                echo '<td>' . $d->Shortcut . '</td>';
                echo '<td>' . $d->Game . '</td>';
                echo '<td>' . ShowSeries($d->ID_League) . '</td>';
                echo $edit_string;
                echo $delete_string;
                echo '</tr>';
            }
        }
        else
        {
            echo 'Keine Liga';
        }
        ?>
        </table>
        </div>
    </body>
</html>